<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * search actions.
 *
 * @package    FinalsClub
 * @subpackage search
 * @author     Masumi
 */
class searchActions extends sfActions {
	public function executeSearch(sfWebRequest $request) {
   		 if (!$query = $request->getParameter('query')) {
      			return;
    		}
 
    		$this->notes = Search::getForLuceneQuery(Search::NOTE,$query);
		$this->notes_resid = Search::cacheResults($this->notes);

		$this->courses = Search::getForLuceneQuery(Search::COURSE,$query);
		$this->courses_resid = Search::cacheResults($this->courses);

		$this->users = Search::getForLuceneQuery(Search::USER,$query);
		$this->users_resid = Search::cacheResults($this->users);

		$this->groups = Search::getForLuceneQuery(Search::GROUP,$query);
		$this->groups_resid = Search::cacheResults($this->groups);

		$this->files = Search::getForLuceneQuery(Search::FILE,$query);
		$this->files_resid = Search::cacheResults($this->files);

		$this->books = Search::getForLuceneQuery(Search::BOOK,$query);
		$this->books_resid = Search::cacheResults($this->books);

		$this->annotations = Search::getForLuceneQuery(Search::ANNOTATION,$query);
		$this->annotations_resid = Search::cacheResults($this->annotations);

		$this->whoami = $this->getUser()->getId();

		$this->query = $request->getParameter('query');
		
  	}

	public function executeShow(sfWebRequest $request) {
		 if (!$query = $request->getParameter('query')) {
      			return;
    		}
		 if (!$type = $request->getParameter('type')) {
      			return;
    		}

		if($start = $request->getParameter('start')) {
			$this->where = $start;
			$this->next = $start + 10;
		} else {
			$this->where = 0;
			$this->next = 10;
		}

		if(!$resid = $request->getParameter('resid')) {
			$this->results = Search::getForLuceneQuery($type,$query);
			$this->size = count($this->results);
			$this->resid = Search::cacheResults($this->results);
		} else {
			$this->results = Search::retrieveCachedResultsReturnDocs($resid,$type,$this->where,$this->next);
			$this->size = count(Search::retrieveCachedResults($resid));
			$this->resid = $resid;
			if(!($this->results)) {
				$this->results = Search::getForLuceneQuery($type,$query);
				Search::cacheResultsWithId($resid, $this->results);
				$this->size = count($this->results);
			}
		}
		
		if($this->size < $this->next) {
			$this->next = $this->size;
		}

		$this->query = $request->getParameter('query');
		switch($type) {
			case Search::NOTE: $this->type = "notes"; break; 
			case Search::COURSE: $this->type = "classes"; break;
			case Search::USER: $this->type = "users"; break;
			case Search::GROUP: $this->type = "groups"; break;
			case Search::FILE: $this->type = "files"; break;
			case Search::BOOK: $this->type = "books"; break;
			case Search::ANNOTATION: $this->type = "annotations"; break;
			default: $this->type = "default"; break;
		}

		$this->whoami = $this->getUser()->getId();
	}
}
